/**
 * @Author ZhangCuirong
 * @Date 2025/10/10 11:44
 * @description:
 */
public class Solution443 {
    public int compress(char[] chars) {
        if (chars.length <= 1) {
            return chars.length;
        }
        char pre = chars[0];
        int count = 1;
        int start = 0;
        for (int i = 1; i < chars.length; i++) {
            if (chars[i] == pre) {
                count++;
            } else {
                chars[start++] = pre;
                if (count != 1) {
                    for (char c : String.valueOf(count).toCharArray()) {
                        chars[start++] = c;
                    }
                }
                pre = chars[i];
                count = 1;
            }
        }
        if (chars[chars.length - 1] == pre) {
            chars[start++] = pre;
            if (count != 1) {
                for (char c : String.valueOf(count).toCharArray()) {
                    chars[start++] = c;
                }
            }
        }
        return start;
    }

    public static void main(String[] args) {
        Solution443 solution = new Solution443();
        System.out.println(solution.compress("aabbccc".toCharArray()));
        System.out.println(solution.compress("a".toCharArray()));
        System.out.println(solution.compress("abbbbbbbbbbbb".toCharArray()));
    }
}
